package com.shop.shoppingmall.controller;

import com.alibaba.fastjson.JSONObject;
import com.shop.shoppingmall.dao.ShUserDao;
import com.shop.shoppingmall.entity.OrderEntity;
import com.shop.shoppingmall.entity.ShInorder;
import com.shop.shoppingmall.entity.ShUser;
import com.shop.shoppingmall.service.ShInorderService;
import com.shop.shoppingmall.util.CommonConstant;
import com.shop.shoppingmall.util.JwtUtil;
import com.shop.shoppingmall.util.api.vo.Result;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSourceExtensionsKt;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * (ShInorder)表控制层
 *
 * @author makejava
 * @since 2021-02-18 16:33:36
 */
@RestController
@CrossOrigin
@RequestMapping("shInorder")
public class ShInorderController {
    /**
     * 服务对象
     */
    @Resource
    private ShInorderService shInorderService;
    @Resource
    private ShUserDao shUserDao;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ShInorder selectOne(String id) {
        return this.shInorderService.queryById(id);
    }

    /**
     * 新增订单
     * @param shInorder
     * @return
     */
    @PostMapping("insertInOrder")
    public Result insertInOrder(HttpServletRequest request,@RequestBody ShInorder shInorder) {
        String name1 = JwtUtil.getUserNameByToken(request);
        ShUser shUser = shUserDao.selectLoginName(name1);
        if(shUser == null || name1 == null){
            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
        }else{
            shInorder.setUserId(shUser.getId());
            return Result.ok(shInorderService.insertInOrder(shInorder));
        }
    }


    /**
     * 查询多条数据
     *
     * @param map 查询起始位置
     * @param map  查询条数
     * @return 对象列表
     */
    @PostMapping("queryAllByLimit")
    public Result queryAllByLimit(HttpServletRequest request,@RequestBody Map map) {
          return Result.ok(shInorderService.queryAllByLimit((int)map.get("offset"),(int)map.get("limit")));
    }

    /**
     * 查询多条数据 app不分页
     * @return 对象列表
     */
    @GetMapping("queryAllByLimit2")
    public Result queryAllByLimit2(HttpServletRequest request) {

        String name1 = JwtUtil.getUserNameByToken(request);
        ShUser shUser = shUserDao.selectLoginName(name1);
        if(shUser == null || name1 == null){
            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
        }else{
            return Result.ok(shInorderService.queryAllByLimit2(shUser.getId()));
        }
    }

    /**
     * 查询详情
     * @param map
     * @return 对象列表
     */
    @PostMapping("inquiryDetails")
    public Result inquiryDetails(HttpServletRequest request,@RequestBody Map map) {

//        String name1 = JwtUtil.getUserNameByToken(request);
//        ShUser shUser = shUserDao.selectLoginName(name1);
//        if(shUser == null || name1 == null){
//            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
//        }else{
            return Result.ok(shInorderService.inquiryDetails((String)map.get("id")));
//        }
    }

    /**
     * 修改数据
     *
     * @param shInorder 实例对象
     * @return 实例对象
     */
    @PostMapping("updateInOrder")
    public Result updateInOrder(HttpServletRequest request,@RequestBody ShInorder shInorder) {
        try{
            shInorderService.update(shInorder);
            return Result.ok(shInorderService.selectByOrderNum(shInorder.getOrderNumber()));
        }catch(Exception e){
            return Result.ok("更新失败，请稍后再试。");
        }

    }
     //支付订单
    @PostMapping("payOrder")
    public Result payOrder(HttpServletRequest request, @RequestBody Map map) {
        String name1 = JwtUtil.getUserNameByToken(request);
        ShUser shUser = shUserDao.selectLoginName(name1);
        if(shUser == null || name1 == null){
            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
        }else{
            String orderNumber = map.get("orderNumber").toString();
            return Result.ok(shInorderService.payOrder(orderNumber,shUser.getId()));
        }
    }
}
